package 并发编程17;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author idea
 * @data 2019/2/17
 */
public class Consumer implements Runnable {


    //阻塞队列
    private BlockingQueue<String> blockingQueue;

    public Consumer(BlockingQueue<String> queue) {
        this.blockingQueue = queue;
    }

    @Override
    public void run() {
        try {
            boolean isRunning = true;
            while (isRunning) {
                String result = blockingQueue.poll(2, TimeUnit.SECONDS);
                if (result != null) {
                    System.out.println("消费者消费--" + result);
                    Thread.sleep(2000);
                } else {
                    //两秒之后还是没有获取数据
                    isRunning=false;
                }
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println("消费者退出--");

        }
    }
}
